package com.arcvideo.acsplayer.utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpResponse;

import android.content.Context;
import android.os.Environment;

public class JsonUtil {
	public static String getJsonStringFromHttpResponse(HttpResponse response) {
		String jsonString = null;
		try {
			InputStream is = response.getEntity().getContent();
			jsonString = getJsonStringFromInputStream(is);
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonString;
	}

	public static String getJsonStringFromInputStream(InputStream iStream) {
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new InputStreamReader(iStream, "UTF-8"), 512 * 1024);
		} catch (UnsupportedEncodingException e1) {

			e1.printStackTrace();
		}
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {

		} finally {
			try {
				iStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
	
	public static String getJsonStringFromAssets(Context context, String fileName) {
		InputStreamReader inputReader = null;
		BufferedReader reader = null;
		try {
			inputReader = new InputStreamReader(context.getResources().getAssets().open(fileName), "UTF-8");
			reader = new BufferedReader(inputReader, 512 * 1024);
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {

		} finally {
			try {
				inputReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
	
	public static String getJsonStringFromFile(Context context, String fileName) {
		InputStreamReader inputReader = null;
		BufferedReader reader = null;
		String filePath = null;
		try {
			boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
			if (sdCardExist) {
				filePath = Environment.getExternalStorageDirectory() + "/" + fileName;
			} else {
				filePath = "/sdcard/" + fileName;
			}
			inputReader = new InputStreamReader(new FileInputStream(filePath), "UTF-8");
			reader = new BufferedReader(inputReader, 512 * 1024);
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {

		} finally {
			try {
				inputReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
}
